﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading;
using System.IO;

namespace PollServer
{
    //创建线程管理器
    public class ThreadMgr
    {
        public ThreadMgr()
        {

        }
        //创建DB线程
        public static void CreateDbThread(GameDbDelegate _gd, object _info, int _sleeps)
        {
            ThreadDb tdb = new ThreadDb(_gd, _info, _sleeps);
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadDb.ThreadProc), tdb);
        }

        //创建推送处理线程
        public static void CreateDealThread(GameDbDelegate _pd, int size)
        {
            for (int i = 0; i < size; i++)
            {
                LockFreeThread lfThread = new LockFreeThread(_pd);
                ThreadPool.QueueUserWorkItem(new WaitCallback(LockFreeThread.run), lfThread);
            }

/*
            MsgThread mt = new MsgThread(MsgQueueType.SWAP_QUEUE);
            ThreadPool.QueueUserWorkItem(new WaitCallback(MsgThread.run), mt);

            MsgThread mtA = new MsgThread(MsgQueueType.WORK_MAIN_QUEUE_A, _pd);
            MsgThread mtB = new MsgThread(MsgQueueType.WORK_MAIN_QUEUE_B, _pd);
            MsgThread mtC = new MsgThread(MsgQueueType.WORK_MAIN_QUEUE_C, _pd);
            MsgThread mtD = new MsgThread(MsgQueueType.WORK_MAIN_QUEUE_D, _pd);
            MsgThread mtE = new MsgThread(MsgQueueType.WORK_MAIN_QUEUE_E, _pd);
            MsgThread mtF = new MsgThread(MsgQueueType.WORK_MAIN_QUEUE_F, _pd);
            MsgThread mtG = new MsgThread(MsgQueueType.WORK_MAIN_QUEUE_G, _pd);
            MsgThread mtH = new MsgThread(MsgQueueType.WORK_MAIN_QUEUE_H, _pd);
            MsgThread mtM = new MsgThread(MsgQueueType.WORK_MAIN_QUEUE_M, _pd);
            ThreadPool.QueueUserWorkItem(new WaitCallback(MsgThread.run), mtA);
            ThreadPool.QueueUserWorkItem(new WaitCallback(MsgThread.run), mtB);
            ThreadPool.QueueUserWorkItem(new WaitCallback(MsgThread.run), mtC);
            ThreadPool.QueueUserWorkItem(new WaitCallback(MsgThread.run), mtD);
            ThreadPool.QueueUserWorkItem(new WaitCallback(MsgThread.run), mtE);
            ThreadPool.QueueUserWorkItem(new WaitCallback(MsgThread.run), mtF);
            ThreadPool.QueueUserWorkItem(new WaitCallback(MsgThread.run), mtG);
            ThreadPool.QueueUserWorkItem(new WaitCallback(MsgThread.run), mtH);
            ThreadPool.QueueUserWorkItem(new WaitCallback(MsgThread.run), mtM);
 */
        }
    }

    //DB线程, 保持对应数据库长链接
    public class ThreadDb
    {
        public GameDbDelegate gameDelegate;
        public object dbInfo;
        public int sleeps = 2000; //毫秒
        //要用到的属性，也就是我们要传递的参数
        public ThreadDb(GameDbDelegate gd, object _info, int _sleeps)
        {
            gameDelegate = gd;
            dbInfo = _info;
            sleeps = _sleeps;
        }

        //保持对数据库长链接
        public static void ThreadProc(object obj)
        {
            ThreadDb tdb = obj as ThreadDb;
            while (tdb.gameDelegate(tdb.dbInfo))
            {
                //延迟200毫秒
                Thread.Sleep(tdb.sleeps);
            }
        }
    }
}
